<?php

namespace api\controller;

use common\controller\VerifyController;
use common\service\OptionsService;
use think\Request;

class OptionsController extends VerifyController
{
    /**
     * @var \common\service\OptionsService
     */
    protected $optionsService;


    public function __construct()
    {
        parent::__construct();
        $this->optionsService = OptionsService::getInstance();
    }

    /**
     * 添加配置
     * @param Request $request
     * @return \think\Response
     */
    public function create(Request $request)
    {
        $param = $request->param();
        $resData = $this->optionsService->addOptionsData($param['data']);
        return json($resData);
    }

    /**
     * 修改options
     * @param Request $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $param = $request->param();
        $resData = $this->optionsService->updateOptionsData($param['data']);
        return json($resData);
    }

    /**
     * 获得指定名称的配置
     * @param Request $request
     * @return \think\Response
     */
    public function getOptions(Request $request)
    {
        $param = $request->param();
        $resData = $this->optionsService->getOptionsData($param["options_name"]);
        return json(success_response('', $resData));
    }

    /**
     * 获取webSocket服务器地址
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function getWebSocketServer(Request $request)
    {
        $data = $this->optionsService->getLogServerStatus();
        if (!empty($data)) {
            $resData["status"] = $data["status"];
            $resData["connect_time"] = $data["connect_time"];
            $resData["websocket_url"] = $data["websocket_url"];
        } else {
            $resData = [];
        }
        return json($resData);
    }

    /**
     * 获取事件服务器地址
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function getLogServerConfig(Request $request)
    {
        $logServerConfig = $this->optionsService->getLogServerStatus();

        if (!empty($logServerConfig)) {
            $logServerConfig["active"] = "yes";
            $logServerConfig["token"] = md5($logServerConfig["access_key"] . $logServerConfig["secret_key"]);
        } else {
            $logServerConfig["active"] = "no";
            $logServerConfig["token"] = "";
        }
        $logServerConfig ['belong_system'] = C('belong_system');
        return json(success_response('', $logServerConfig));
    }

    /**
     * 保存域登录服务配置
     * @param Request $request
     * @return \think\Response
     */
    public function saveLdapSetting(Request $request)
    {
        $param = $request->param();
        $optionsService = OptionsService::getInstance();
        $resData = $optionsService->saveLdapSetting($param);
        return json($resData);
    }

    /**
     * 获取任务状态列表
     * @param Request $request
     * @return \think\Response
     */
    public function getTaskStatusList(Request $request)
    {
        $optionsService = OptionsService::getInstance();
        $resData = $optionsService->getTaskStatusList();
        return json(success_response('', $resData));
    }

    /**
     * 查询消息模板id
     * @param Request $request
     * @return \think\Response
     */
    public function getNotifyTemplateId(Request $request)
    {
        $resData = $this->optionsService->getNotifyTemplateId($request->param()["item_name"]);
        return json(success_response('', $resData));
    }

    /**
     * 获得指定名称的配置
     * @param Request $request
     * @return \think\Response
     */
    public function getOptionsItem(Request $request)
    {
        $param = $request->param();
        $resData = $this->optionsService->getOptionsConfigItemData($param["options_name"], $param["options_item_name"]);
        return json(success_response('', $resData));
    }
}
